package com.uni.bgController;

import com.uni.common.util.PageBean;
import com.uni.common.util.ReturnData;
import com.uni.entity.Professor;
import com.uni.entity.School;
import com.uni.service.impl.ProfessorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/professor")
public class ProfessorController {


    String bath_url="http://106.13.225.125:90/";

    @Autowired
    ProfessorService professorService;

    @GetMapping("/findList")
    public ReturnData findList(PageBean<Professor> pageBean,String name,String keywords){
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(name)){
            map.put("name",name);
        }if (StringUtils.isNotEmpty(keywords)){
            map.put("keywords",keywords);
        }
        pageBean.setParam(map);
        List<Professor> list = professorService.getProfessorList(pageBean);
        pageBean.setContent(list);
        Integer integer = professorService.countList(pageBean);
        pageBean.setTotalCount(integer);
        return new ReturnData().okBack(pageBean);
    }

    @PostMapping("/update")
    public ReturnData update(Professor professor){
        if (StringUtils.isNotEmpty(professor.getImgUrl())){
            professor.setImgUrl(bath_url+professor.getImgUrl());
        }
        Integer integer = professorService.updatePro(professor);
        if (integer>0){
            return new ReturnData().okBack();
        }
        return new ReturnData().failBack();
    }

    @RequestMapping("/delete")
    public ReturnData delete(String ids) {
        ReturnData rd = new ReturnData();
        if(StringUtils.isBlank(ids)) {
            return rd.failBack("请选择要删除的内容");
        }
        int rs =professorService.delete(Arrays.asList(ids.split(",")));
        if (rs==0){
            return rd.failBack("没有删除任何内容");
        }
        return rd.okBack("共删除"+rs+"条记录");
    }

    @PostMapping("/add")
    public ReturnData insert(Professor professor) {
        ReturnData rd = new ReturnData();
        if (!StringUtils.isNotEmpty(professor.getImgUrl())){
            return rd.failBack("请添加图片");
        }


        Integer insert = professorService.insert(professor);
        if(insert<0) {
            return rd.failBack("添加失败");
        }
        else {
            return rd.okBack("操作成功");
        }

    }
}
